Completed
Push — master ( 6d55f5...924656 )
by Maxence
07:08
created

Navigate.displayPanels   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 9.4285
1
/*
2
 * FullTextSearch - Full text search framework for Nextcloud
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2018
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
27
/** global: OCA */
28
/** global: _ */
29
30
const fullTextSearch = OCA.FullTextSearch.api;
0 ignored issues
show
Backwards Compatibility introduced by
'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
31
32
33
var elements = {
34
	searchTimeout: null,
35
	search_input: null,
36
	search_submit: null,
37
	search_json: null
38
};
39
40
const Navigate = function () {
0 ignored issues
show
Backwards Compatibility introduced by
'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
41
	this.init();
42
};
43
44
Navigate.prototype = {
45
46
	init: function () {
47
		var self = this;
48
49
		fullTextSearch.setEntryTemplate($('#template_entry'), self);
50
		fullTextSearch.setResultContainer($('#search_result'));
51
52
		elements.search_input = $('#search_input');
53
		elements.search_submit = $('#search_submit');
54
		elements.search_panels = $('#search_navigation');
55
//		elements.search_json = $('#search_json');
56
		elements.divHeader = $('#search_header');
57
58
		elements.search_input.on('input', function () {
59
			self.resetSearch();
60
			if (elements.searchTimeout === null && self.initSearch(false)) {
61
				elements.searchTimeout = _.delay(function () {
62
					self.initSearch(false);
63
					elements.searchTimeout = null;
64
				}, 3000);
65
			}
66
		});
67
68
		//
69
		// $(document).keypress(function (e) {
70
		// 	if (e.which === 13) {
71
		// 		self.initSearch(true);
72
		// 	}
73
		// });
74
75
		self.initPanels();
76
	},
77
78
79
	initPanels: function () {
80
		var self = this;
81
82
		$.ajax({
83
			method: 'GET',
84
			url: OC.generateUrl('/apps/fulltextsearch/navigation/panels')
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
85
		}).done(function (res) {
86
			self.displayPanels(res);
87
		});
88
	},
89
90
91
	displayPanels: function (data) {
92
93
		var ak = Object.keys(data);
94
		for (var i = 0; i < ak.length; i++) {
95
			var title = data[ak[i]]['title'];
0 ignored issues
show
Coding Style introduced by
['title'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
96
			var nav = data[ak[i]]['navigation'];
0 ignored issues
show
Coding Style introduced by
['navigation'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
97
98
			var li = $('<li>', {class: (nav.options !== undefined) ? 'collapsible open' : ''});
99
			var aIcon = $('<a>', {
100
				href: '#',
101
				class: 'search_icon'
102
			});
103
			aIcon.text(title);
104
105
			var ul = $('<ul>');
106
107
108
			if (nav.options !== undefined) {
109
110
				aIcon.on('click', function () {
0 ignored issues
show
Bug introduced by
It is generally not recommended to make functions within a loop.

While making functions in a loop will not lead to any runtime error, the code might not behave as you expect as the variables in the scope are not imported by value, but by reference. Let’s take a look at an example:

var funcs = [];
for (var i=0; i<10; i++) {
    funcs.push(function() {
        alert(i);
    });
}

funcs[0](); // alert(10);
funcs[1](); // alert(10);
/// ...
funcs[9](); // alert(10);

If you would instead like to bind the function inside the loop to the value of the variable during that specific iteration, you can create the function from another function:

var createFunc = function(i) {
    return function() {
        alert(i);
    };
};

var funcs = [];
for (var i=0; i<10; i++) {
    funcs.push(createFunc(i));
}

funcs[0](); // alert(0)
funcs[1](); // alert(1)
// ...
funcs[9](); // alert(9)
Loading history...
111
					var li = $(this).closest('li');
112
					if (li.hasClass('open')) {
113
						li.removeClass('open');
114
					} else {
115
						li.addClass('open');
116
					}
117
				});
118
119
				for (var j = 0; j < nav.options.length; j++) {
120
					var sub = nav.options[j];
121
122
					var subA = $('<a>', {
123
						href: '#',
124
						text: sub.title
125
					});
126
127
					if (sub.type === 'checkbox') {
128
						ul.append($('<li>').append(subA).append($('<input>', {
129
							class: 'search_checkbox_sub',
130
							type: 'checkbox'
131
						})));
132
					}
133
				}
134
			}
135
136
			li.append(aIcon);
137
			li.append($('<input>', {
138
				class: 'search_checkbox',
139
				type: 'checkbox'
140
			}));
141
			li.append(ul);
142
143
			elements.search_panels.append(li);
144
		}
145
146
	},
147
148
149
	initSearch: function (force) {
150
		var search = elements.search_input.val();
151
152
		if (!force && search.length < 3) {
153
			return false;
154
		}
155
		var request = {
156
			providers: 'all',
157
			search: search,
158
			page: curr.page
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
159
		};
160
161
		fullTextSearch.search(request, this.searchResult);
162
163
		return true;
164
	},
165
166
167
	resetSearch: function () {
168
		// if (elements.search_input.val() !== '') {
169
		// 	return;
170
		// }
171
	},
172
173
174
	searchResult: function (result) {
175
176
		if (elements.search_json !== null) {
177
			elements.search_json.text(JSON.stringify(result));
178
		}
179
180
		// console.log(JSON.stringify(result));
181
//			OCA.notification.onFail('Search returned no result');
182
//		OCA.notification.onSuccess('Search returned ' + res.meta.size + ' result(s)');
183
184
	},
185
186
187
	onEntryGenerated: function (entry) {
188
		this.deleteEmptyDiv(entry, '#line1');
189
		this.deleteEmptyDiv(entry, '#line2');
190
	},
191
192
193
	deleteEmptyDiv: function (entry, divId) {
194
		var div = entry.find(divId);
195
		if (div.text() === '') {
196
			div.remove();
197
		}
198
	}
199
};
200
201
OCA.FullTextSearch.Example = Navigate;
202
203
204
$(document).ready(function () {
205
	OCA.FullTextSearch.example = new Navigate();
206
});
207
208
209
210